package com.znsb.msfq.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.adapter.MyMessageRVAdapter;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.bean.MessageBean;
import com.znsb.msfq.utils.GsonUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.UrlUtils;
import com.znsb.msfq.view.EndLessOnScrollListener;
import com.znsb.msfq.view.LayoutView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/10
 * @modifyTime： 2017/2/10
 * @explain：
 */

public class MyMessageActivity extends BaseActivity {

    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.list_recyler_view)
    RecyclerView listRecylerView;
    @Bind(R.id.list_swiperefresh_layout)
    SwipeRefreshLayout listSwiperefreshLayout;
    @Bind(R.id.list_layoutview)
    LayoutView listLayoutview;

    private MyMessageRVAdapter adapter;
    private LinearLayoutManager layoutManager;
    private int pageNo = 1;

    @Override
    protected int getlayoutView() {
        return R.layout.list_recyler_view;
    }

    @Override
    protected void initView(View view) {
        barTvTitle.setText("消息中心");

        /**
         * 列表属性配置
         */
        listSwiperefreshLayout.setColorSchemeResources(R.color.blueStatus);
        listSwiperefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                listSwiperefreshLayout.setRefreshing(true);
            }
        });

        layoutManager = new LinearLayoutManager(this);
        listRecylerView.setLayoutManager(layoutManager);
        listRecylerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        listSwiperefreshLayout.setOnRefreshListener(onRefresh);
        /**
         * 下拉加载更多
         */
        listRecylerView.addOnScrollListener(new EndLessOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                pageNo++;
                getMessageList(pageNo);
            }
        });
        adapter = new MyMessageRVAdapter(this, list);
        listRecylerView.setAdapter(adapter);
        listLayoutview.setVisibility(View.GONE);
    }


    @Override
    protected void initData() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                pageNo = 1;
                list.clear();
                getMessageList(pageNo);
                listSwiperefreshLayout.setRefreshing(false);
            }
        }, 1000);
    }

    @Override
    protected void clickListener(View view) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private List<MessageBean.DataListBean> list = new ArrayList<>();
    private List<MessageBean.DataListBean> dataList = new ArrayList<>();

    private void getMessageList(final int pageNo) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("pageNo", pageNo);
        maps.put("pageSize", 10);
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_MESSAGES, HttpUtils.getLoginMaps(maps), new ResponseUtils() {
            @Override
            public void onSucceed(String Code, JSONObject request) {
                LogUtils.i("TAG:", "getMessageList=" + request.toString());
                MessageBean data = (MessageBean) GsonUtils.getGsonData(MessageBean.class, request.toString());
                if (data != null) {
                    dataList = data.getDataList();
                    Message message = new Message();
                    message.obj = dataList;
                    message.what = 100;
                    handler.sendMessageDelayed(message, 100);
                }

            }

            @Override
            public void onFailure(String result) {

            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 100:
                    dataList = (List<MessageBean.DataListBean>) msg.obj;
                    if (dataList != null && dataList.size() > 0)
                        list.addAll(dataList);
                    adapter.updateList(list);
                    if (list != null && list.size() > 0) {
                        listLayoutview.setVisibility(View.GONE);
                    } else {
                        listLayoutview.setVisibility(View.VISIBLE);
                        listLayoutview.setEmptyDrawable(R.mipmap.messagecenter_icon);
                        listLayoutview.showEmpty(R.mipmap.messagecenter_icon, "暂无消息数据");
                    }
                    break;
            }

        }
    };
    /**
     * 下拉刷新
     */
    private SwipeRefreshLayout.OnRefreshListener onRefresh = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    pageNo = 1;
                    list.clear();
                    getMessageList(pageNo);
                    listSwiperefreshLayout.setRefreshing(false);
                }
            }, 1000);
        }
    };

    @OnClick(R.id.bar_img_back)
    public void onClick() {
        finish();
    }

    /**
     * 列表item点击事件
     */

}
